"""
运行策略示例
！！！需要在执行完成backtest之后运行
"""
import time

import pandas as pd

from config import backtest_path, raw_data_path
from core.equity import calc_equity
from core.model.backtest_config import BacktestConfig
from core.select_coin import agg_multi_strategy_ratio
from core.utils.log_kit import logger

logger.info('init...')
conf = BacktestConfig.init_from_config()
select_results = pd.read_pickle(backtest_path / 'final_select_results.pkl')
all_factors_kline = pd.read_pickle(backtest_path / 'all_factors_kline.pkl')
logger.ok('inited.')

pivot_dict_spot = pd.read_pickle(raw_data_path / 'market_pivot_spot.pkl')
pivot_dict_swap = pd.read_pickle(raw_data_path / 'market_pivot_swap.pkl')

if __name__ == '__main__':
    # ====================================================================================================
    # 6. 添加下一个每一个周期需要卖出的币的信息
    # ====================================================================================================
    s_time = time.time()
    logger.info(f'开始权重聚合...')
    df_spot_ratio, df_swap_ratio = agg_multi_strategy_ratio(conf, select_results)
    logger.ok(f'完成权重聚合，花费时间： {time.time() - s_time:.3f}秒')

    # ====================================================================================================
    # 7. 根据选币结果计算资金曲线
    # ====================================================================================================
    s_time = time.time()
    logger.info(f'开始模拟交易，累计回溯 {len(df_spot_ratio):,} 小时（~{len(df_spot_ratio) / 24:,.0f}天）...')
    calc_equity(conf, pivot_dict_spot, pivot_dict_swap, df_spot_ratio, df_swap_ratio)
    logger.ok(f'完成模拟交易，花费时间：{time.time() - s_time:.3f}秒')
